<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<div id="user_content_div">
	<div id="user_condition_div">
		<table style="width:100%; border-collapse:separate; border-spacing:5px;">
			<tr>
				<td>
					<input id="userCode">
				</td>
				<td>
					<input id="userName">
				</td>
				<td>
					<input id="email">
				</td>
			</tr>
			<tr>
				<td colspan="3">
					<a id="user_search_btn" href="javascript:void(0);"></a>
					<a id="user_role_btn" href="javascript:void(0);"></a>
				</td>
			</tr>
		</table>
	</div>
	<div id="user_list_div">
		<table id="user_list_table"></table>
	</div>
</div>
<script type="text/javascript">
	$(function() {
		initUserConditionDiv();
		initUserListTable();
	});
	
	/** 初始化用户页面的条件DIV和用户列表DIV  */
	function initUserConditionDiv() {
		$("#user_condition_div").panel({
			title : "查询条件",
			border : false
		});
		$("#user_list_div").panel({
			title : "用户列表",
			border : false
		});
		$("#userCode").textbox({
			label : "编码：",
			labelAlign : "right",
			width : "300px",
			labelWidth : "50px"
		});
		$("#userName").textbox({
			label : "姓名：",
			labelAlign : "right",
			width : "300px",
			labelWidth : "50px"
		});
		$("#email").textbox({
			label : "邮箱：",
			labelAlign : "right",
			width : "300px",
			labelWidth : "50px"
		});
		$("#user_search_btn").linkbutton({
			text : "查询",
			iconCls : "icon-zoom",
			onClick : function() {
				initUserListTable();
			}
		});
		$("#user_role_btn").linkbutton({
			text : "添加角色",
			iconCls : "icon-drive_user",
			onClick : function() {
				$.messager.alert("提示信息", "添加角色功能待实现", "info");
			}
		});
	}
	
	/** 初始化用户列表TABLE  */
	function initUserListTable() {
		var params = {};
		params.userCode = $("#userCode").textbox("getValue");
		params.userName = $("#userName").textbox("getValue");
		params.email = $("#email").textbox("getValue");
		$("#user_list_table").datagrid({
			url : "${pageContext.request.contextPath}/system/initUserTable",
			pagination : true,
			singleSelect : true,
			width : "auto",
			height : "auto",
			fitColumns : true,
			pageList : [10,20,30,40,50],
			queryParams : params,
			idField : "userId",
			columns : [[
				{field:"userId",title:"用户ID",width:50},
				{field:"comName",title:"机构名称",width:100},
				{field:"userCode",title:"用户编码",width:100},
				{field:"userName",title:"姓名",width:50},
				{field:"email",title:"邮箱",width:150},
				{field:"rcState",title:"状态",width:50},
				{field:"createDate",title:"创建时间",width:150},
				{field:"modifyDate",title:"修改时间",width:150},
				{field:"createUserName",title:"创建人",width:100},
				{field:"modifyUserName",title:"修改人",width:100}
			]],
			toolbar : [{
				iconCls : "icon-add",
				handler : function() {
					$("#user_content_div").append("<div id='user_add_dialog'></div>");
					$("#user_add_dialog").dialog({
						title : "添加用户",
						closable : false,
						modal : true,
						width : "800px",
						height : "250px",
						cache : false,
						href : "${pageContext.request.contextPath}/system/userAddPage",
						buttons : [{
							text : "添加",
							handler : function() {
								var userName = $.trim($("#user_add_userName").textbox("getValue"));
								var userCode = $.trim($("#user_add_userCode").textbox("getValue"));
								var comId = $("#user_add_company").combobox("getValue");
								if ("" == userName || "" == userCode || "" == comId) {
									$.messager.alert("提示信息", "姓名、编码、机构必填！", "error");
									return;
								}
								var email = $.trim($("#user_add_email").textbox("getValue"));
								
								var user = {};
								user.userName = userName;
								user.userCode = userCode;
								user.email = email;
								if ("" != $("#user_child2_company").combobox("getValue")) {
									user.comId = $("#user_child2_company").combobox("getValue");
								} else if ("" != $("#user_child1_company").combobox("getValue")) {
									user.comId = $("#user_child1_company").combobox("getValue");
								} else {
									user.comId = comId;
								}
								$.post("${pageContext.request.contextPath}/system/addUser",
									user,
									function(data) {
										if (data.flag) {
											$.messager.alert("提示信息", data.msg, "info");
											$("#user_add_dialog").dialog("destroy");
											initUserListTable();
										} else {
											$.messager.alert("异常信息", data.msg, "error");
										}
									},"JSON"
								);
							}
						}, {
							text : "取消",
							handler : function() {
								$("#user_add_dialog").dialog("destroy");
							}
						}]
					});
				}
			}, {
				iconCls : "icon-edit",
				handler : function() {
					var selectedRow = $("#user_list_table").datagrid("getSelected");
					if (null == selectedRow) {
						$.messager.alert("异常信息", "请选择要修改的行！", "error");
						return;
					}
					$("#user_content_div").append("<div id='user_modify_dialog'></div>");
					$("#user_modify_dialog").dialog({
						title : "修改用户",
						closable : false,
						modal : true,
						width : "800px",
						height : "250px",
						cache : false,
						href : "${pageContext.request.contextPath}/system/userModifyPage",
						buttons : [{
							text : "修改",
							handler : function() {
								var userName = $.trim($("#user_modify_userName").textbox("getValue"));
								var comId = $("#user_modify_company").combobox("getValue");
								if (userName == "" || userCode == "" || comId == "") {
									$.messager.alert("提示信息", "姓名、机构必填！", "error");
									return;
								}
								var email = $("#user_modify_email").textbox("getValue");
								
								var user = {};
								user.userName = userName;
								user.email = email;
								if ("" != $("#user_modify_company_child2").combobox("getValue")) {
									user.comId = $("#user_modify_company_child2").combobox("getValue");
								} else if ("" != $("#user_modify_company_child1").combobox("getValue")){
									user.comId = $("#user_modify_company_child1").combobox("getValue");
								} else {
									user.comId = comId;
								}
								user.userId = selectedRow.userId;
								
								$.post("${pageContext.request.contextPath}/system/modifyUser",
									user,
									function(data) {
										if (data.flag) {
											$.messager.alert("提示信息", data.msg, "info");
											$("#user_modify_dialog").dialog("destroy");
											initUserListTable();
										} else {
											$.messager.alert("异常信息", data.msg, "error");
										}
									},"JSON"
								);
							}
						}, {
							text : "取消",
							handler : function() {
								$("#user_modify_dialog").dialog("destroy");
							}
						}]
					});
				}
			}, {
				iconCls : "icon-delete",
				handler : function() {
					var selectedRow = $("#user_list_table").datagrid("getSelected");
					if (null == selectedRow) {
						$.messager.alert("异常信息", "请选择要删除的行！", "error");
						return;
					}
					$.messager.confirm("确认对话框", "您确实要删除这条数据吗？", function(r) {
						if (r) {
							$.post("${pageContext.request.contextPath}/system/deleteUser",
								{userId : selectedRow.userId},
								function(data) {
									if (data.flag) {
										$.messager.alert("提示信息", data.msg, "info");
										initUserListTable();
									} else {
										$.messager.alert("异常信息", data.msg, "error");
									}
								},"JSON"
							);
						}
					});
				}
			}]
		});
	}
</script>